package com.demo2do.fms.admin.dao.vehicle;

import java.util.Date;
import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.demo2do.fms.entity.vehicle.source.RemoteTransMeans;
import com.demo2do.fms.entity.vehicle.source.VehicleGpsInfo;

/**
 * 
 * @author mohanlan
 *
 */
@Dao("hibernate")
public interface RemoteTransMeansDao {
    
    /**
     * 
     * @param date
     * @return
     */
    @Query("SELECT vid FROM VehicleGpsInfo WHERE lastModified < :lastModified")
    public List<Long> listNeedLockVids(@Variable("lastModified") Date date);
    
    /**
     * 
     * @param date
     * @return
     */
    @Query("SELECT vid FROM VehicleGpsInfo WHERE lastModified > :lastModified")
    public List<Long> listNeedUnlockVids(@Variable("lastModified") Date date);
    
    /**
     * 
     * @return
     */
    @Query("SELECT means.id FROM RemoteTransMeans means LEFT JOIN VehicleGpsInfo info ON means.id = info.vid WHERE info.vid IS NULL")
    public List<Long> listNoGpsVids();
    
    /**
     * 
     * @return
     */
    @Query("SELECT vs.code FROM VehicleSource vs LEFT JOIN VehicleDriverRelation relation ON relation.vehicleCode = vs.code WHERE relation.vehicleCode IS NULL AND vs.code IS NOT NULL")
    public List<String> listNoRelationVCodes();
    
    /**
     * 
     * @return
     */
    @Query("SELECT DISTINCT(relation.vehicleCode) FROM VehicleDriverRelation relation WHERE relation.driverId IS NULL")
    public List<String> listNoDriverVCodes();
    
    /**
     * @param unlockDate
     * @return
     */
    @Query("FROM RemoteTransMeans WHERE unlockDate > :unlockDate AND disabled = false")
    public List<RemoteTransMeans> listByUnlockDate(@Variable("unlockDate") Date unlockDate);
    
    /**
     * 
     * @param id
     * @return
     */
    @Query("FROM RemoteTransMeans WHERE disabled = false AND id = :id")
    public RemoteTransMeans load(@Variable("id") Long id);
    
    /**
     * @param code
     * @return
     */
    @Query("FROM RemoteTransMeans WHERE disabled = false AND code = :code")
    public RemoteTransMeans loadByCode(@Variable("code") String code);
    
    /**
     * @param vehicleId
     * @param lastModified
     * @return
     */
    @Query("FROM VehicleGpsInfo WHERE vid = :vid AND lastModified > :lastModified")
    public List<VehicleGpsInfo> listLatestGps(@Variable("vid") Long vehicleId, @Variable("lastModified") Date lastModified);
    
}
